package com.king.headline.util.umeng;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;

import com.google.gson.Gson;
import com.king.headline.UI.MainUI;
import com.king.headline.UI.SettingActivity;
import com.king.headline.beans.UserInfo;
import com.king.headline.cache.CacheUtils;
import com.king.headline.net.Callback;
import com.king.headline.net.NetWork;
import com.king.headline.util.AccountManager;
import com.king.headline.util.CustomToast;
import com.king.headline.util.Utils;
import com.umeng.socialize.UMAuthListener;
import com.umeng.socialize.UMShareAPI;
import com.umeng.socialize.bean.SHARE_MEDIA;
import org.json.JSONObject;
import java.util.Map;

/**
 * Created by king on 2016/6/29.
 */
public class LoginThreeUtils {
    private int manageType;//1-->登录,2-->注册,3-->反注册
    private Activity mActivity;
    private SHARE_MEDIA platform;
    private UMShareAPI mShareAPI;
    private ProgressDialog pDialog;
    private String encyptdId="";
    private String apikey="";
    private String threeJson;

    public LoginThreeUtils(Activity activity, UMShareAPI mShareAPI) {
        this.mActivity = activity;
        this.mShareAPI = mShareAPI;
    }
    public void setManageType(int manageType){
        this.manageType = manageType;
    }

    public void loginThree( SHARE_MEDIA platform){
        this.platform = platform;
        mShareAPI.doOauthVerify(mActivity, platform, umAuthListener);
    }

    private UMAuthListener umAuthListener =new UMAuthListener() {
        @Override
        public void onComplete(SHARE_MEDIA share_media, int i, Map<String, String> map) {
            if (i == 0){
                mShareAPI.getPlatformInfo(mActivity, platform, umAuthListener);
            }else {
                Login3ToNet(map,share_media);
            }
        }

        @Override
        public void onError(SHARE_MEDIA share_media, int i, Throwable throwable) {

        }

        @Override
        public void onCancel(SHARE_MEDIA share_media, int i) {

        }
    };

    private void Login3ToNet(Map<String, String> map, SHARE_MEDIA share_media) {
        pDialog = ProgressDialog.show(mActivity, "", "请稍候~");
        UserInfo userInfo = AccountManager.getInstance().getUser();
        if (userInfo!=null){
            encyptdId = userInfo.encyptdId;
            apikey = userInfo.apikey;
        }
        NetWork.getInstance().login3rd(map,encyptdId,share_media, new Callback() {
            @Override
            public void success(String result) {
                Utils.dismissDialog(pDialog);
                if (manageType==1) {//登录
                    Gson gson = new Gson();
                    UserInfo userInfo = gson.fromJson(result, UserInfo.class);
                    AccountManager.getInstance().setUser(userInfo);
                    CacheUtils.putString(CacheUtils.APIKEY, userInfo.apikey);
                    mActivity.startActivity(new Intent(mActivity, MainUI.class));
                    mActivity.finish();
                }else if (manageType==2) {//注册
                    ((SettingActivity)mActivity).login();
                }else if (manageType==3) {//反注册

                }
            }

            @Override
            public void notice(JSONObject jsonObject, String message) {
                Utils.dismissDialog(pDialog);
                CustomToast.showToast(message);
            }

            @Override
            public void failed(String e) {
                Utils.dismissDialog(pDialog);
                CustomToast.showToast(e);
            }
        });
    }
    public void LoginRemoveBind3ToNet(SHARE_MEDIA share_media) {
        pDialog = ProgressDialog.show(mActivity, "", "请稍候~");
        UserInfo userInfo = AccountManager.getInstance().getUser();
        if (userInfo!=null){
            encyptdId = userInfo.encyptdId;
            apikey = userInfo.apikey;
            if (share_media.equals(SHARE_MEDIA.QQ)) {
                threeJson = userInfo.qqJson;
            }
        }
        NetWork.getInstance().LoginRemoveBind(threeJson,encyptdId,apikey,share_media, new Callback() {
            @Override
            public void success(String result) {
                Utils.dismissDialog(pDialog);
                CustomToast.showToast("操作成功");
                ((SettingActivity)mActivity).login();
            }

            @Override
            public void notice(JSONObject jsonObject, String message) {
                Utils.dismissDialog(pDialog);
                CustomToast.showToast(message);
            }

            @Override
            public void failed(String e) {
                Utils.dismissDialog(pDialog);
                CustomToast.showToast(e);
            }
        });
    }

}
